package com.randi.system.service.impl;

import com.randi.common.config.AppConfig;
import com.randi.common.constant.CacheConstants;
import com.randi.common.core.domain.vo.SaltInfo;
import com.randi.common.utils.RedisUtil;
import com.randi.common.utils.secret.SaltUtils;
import com.randi.system.service.ISysSaltService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author ziye
 */
@Service
public class SysSysSaltServiceImpl implements ISysSaltService {

    private Integer saltExpireTime;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private void init(AppConfig.SystemConfig.SecretConfig secretConfig) {
        saltExpireTime = secretConfig.getExpireTime();
    }

    /**
     * 构建加密盐
     *
     * @return salt
     */
    @Override
    public SaltInfo createSaltInfo() {
        SaltInfo saltInfo = SaltUtils.buildSaltInfo();
        redisUtil.setCache(CacheConstants.SALT_KEY + saltInfo.getUuid(), saltInfo, saltExpireTime, TimeUnit.MINUTES);
        return saltInfo;
    }

    /**
     * 获取加密盐数据
     *
     * @param uuid uuid
     * @return salt
     */
    @Override
    public SaltInfo getSaltInfo(String uuid) {
        return redisUtil.getCache(CacheConstants.SALT_KEY + uuid);
    }

    /**
     * 移除加密盐数据
     *
     * @param uuid
     */
    @Override
    public void removeSaltInfo(String uuid) {
        if (redisUtil.hasKey(CacheConstants.SALT_KEY + uuid)) {
            redisUtil.delete(CacheConstants.SALT_KEY + uuid);
        }
    }

}
